<template>
  <div class="editChannle">
    <!-- 我的频道 -->
    <van-cell class="myCell">
      <span class="channle-title" slot="title">我的频道</span>
      <van-button @click="isEdit = !isEdit" round size="mini" plain color="#7232dd" type="default">{{isEdit? '完成':'编辑'}}
      </van-button>
    </van-cell>
    <!-- 我的频道列表 -->
    <van-grid :gutter="10" class="listGrid myGrid">
      <van-grid-item @click="dataEditClick(index,item.id)" :class="{ active: index===active}"
        v-for="(item,index) in channels" :key="item.id" :icon="(isEdit &&index !==0) ? 'close':''" :text="item.name" />
    </van-grid>
    <!-- 推荐频道 -->
    <van-cell :boder="false" class="recommend-cell">
      <span class="channle-title" slot="title">推荐标题</span>
    </van-cell>
    <!-- 推荐频道列表 -->
    <van-grid direction="horizontal" :gutter="10" class="listGrid recommend-grid">
      <van-grid-item @click="clickRecommend(item)" v-for="item of recommendChannels" :key="item.id" icon="plus"
        :text="item.name" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannels, delUserChannels } from '@/api/channels/index.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
const HMTT = 'hmtt_channel'
export default {
  name: 'editCC',
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  methods: {
    // 获取所有频道数据
    async loadChannels () {
      try {
        const { data: res } = await getAllChannels()
        console.log(res)
        this.allChannels = res.data.channels
      } catch (err) {
        this.$toast('获取失败')
      }
    },
    // 添加到我的频道列表，移除推荐列表,持久化-----------------
    async clickRecommend (item) {
      this.channels.push(item)
      if (this.user) {
        // 已登录
        try {
          await addUserChannels({
            id: item.id,
            seq: this.channels.length
          })
          this.$toast.success('添加成功')
        } catch {
          this.$toast('添加失败')
        }
      } else {
        // 未登录
        setItem(HMTT, this.channels)
      }
    },

    // 跳转,移除时间
    dataEditClick (index, id) {
      if (this.isEdit) {
        if (index === 0) return
        // 编辑状态的逻辑
        if (index < this.active) {
          this.$emit('sendData', {
            close: true,
            index: this.active - 1
          })
        }
        this.channels.splice(index, 1)
        this.deleteChannelStorage(id)
      } else {
        // 未编辑 状态逻辑
        this.$emit('sendData', {
          close: false,
          index
        })
      }
    },
    // 删除数据的持久化------------------
    async deleteChannelStorage (id) {
      if (this.user) {
        try {
          // 调用删除接口
          await delUserChannels(id)
          this.$toast.success('删除成功')
        } catch {
          this.$toast('删除失败')
        }
      } else {
        // 数据存到数据库
        setItem(HMTT, this.channels)
      }
    }
  },
  computed: {
    // 获取推荐频道列表
    recommendChannels () {
      const arr = this.allChannels.filter(item => {
        const findRes = this.channels.find(data => data.id === item.id)
        if (findRes) return false
        return true
      })
      return arr
    },
    // 拿到vuex的user登录状态
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    // 调用获取所有频道数据
    this.loadChannels()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.editChannle {
  padding-top: 60px;
  // myCell样式
  .myCell {
    align-items: center;
    &::after {
      border: none;
    }
    .channle-title {
      font-size: 32px;
      color: #333;
    }
    .van-button--default {
      padding: 5px 20px;
    }
  }
  // 我的频道宫格（图标）
  /deep/.myGrid {
    .van-grid-item__icon + .van-grid-item__text {
      margin-top: 0px;
    }
    .van-icon-close {
      font-size: 34px;
      position: absolute;
      right: -13px;
      top: -13px;
    }
    .active {
      .van-grid-item__text {
        color: red;
      }
    }
  }
  // 公共list 样式
  /deep/ .listGrid {
    .van-grid-item {
      height: 86px;
      .van-grid-item__content {
        border-radius: 6px;
        background-color: #f4f5f6;
        &::after {
          border: none;
        }
      }
    }
  }
  // 推荐cell单元
  .recommend-cell {
    .channle-title {
      font-size: 32px;
      color: #333;
    }
  }
  // 推荐频道宫格
  /deep/.recommend-grid {
    // +小图标大小
    .van-grid-item__icon {
      font-size: 24px;
    }
  }
}
</style>
